package com.tanhua.manage.controller;


import com.tanhua.common.utils.NoAuthorization;
import com.tanhua.manage.service.MessageService;
import com.tanhua.manage.service.VideoService;
import com.tanhua.manage.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
//@RequestMapping("management/manage/messages")
public class MessageController {


    @Autowired
    private MessageService messageService;
    /**
     * 动态记录翻页
     * @param page
     * @param pagesize
     * @param sortProp   排序字段
     * @param sortOrder  ascending 升序 descending 降序
     * @param uid
     * @return
     */
    @GetMapping

    public ResponseEntity<PageResult> queryMessageListById(@RequestParam(name = "page")Integer page,
                                                     @RequestParam(name = "pagesize")Integer pagesize,
                                                     @RequestParam(name = "sortProp")String sortProp,
                                                     @RequestParam(name = "sortOrder")String sortOrder,
                                                     @RequestParam(name = "uid")Long uid){
        try {
            if (page<=0){
                page=1;
            }

            PageResult pageResult = this.messageService.queryMessageListById(page, pagesize, uid, sortOrder, sortProp);

            if (null!=pageResult){
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 根据动态ID查询动态
     * @param token
     * @param messageID
     * @param pageSize
     * @param page
     * @param sortProp
     * @param sortOrder
     * @return
     */
    @GetMapping("{messageID}")
    public ResponseEntity<PageResult> queryMessagesByMessageId(@RequestHeader(name = "Authorization") String token,
                                                               @PathVariable(name="messageID") String messageID,
                                                               @RequestParam(name = "pagesize",defaultValue = "10") Integer pageSize,
                                                               @RequestParam(name = "page",defaultValue = "1") Integer page,
                                                               @RequestParam(name = "sortProp",required = false) String sortProp,
                                                               @RequestParam(name = "sortOrder",defaultValue = "descending ") String sortOrder){



        try {
            PageResult pageResult = this.messageService.queryMessagesByMessageId(page,pageSize,messageID,sortProp,sortOrder);
            if (null != pageResult){
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();

    }
}
